///////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2010 Chris Colinsky
// All Rights Reserved
//
// version	0.5
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////

package sugarcookie.browser {
	import flash.external.ExternalInterface;
	
	import mx.utils.ObjectUtil;
	import mx.collections.ArrayCollection;
	
	/**
	 * Manages browser popup windows. Requires sugarcookie.js
	 */
	public class Popup {
		//--------------------------------------------------------------------------
	    //
	    //  Class properties
	    //
	    //--------------------------------------------------------------------------
	    
	    /**
		 * @private
		 */
		private static var _windows:ArrayCollection = new ArrayCollection();
		
		//--------------------------------------------------------------------------
	    //
	    //  Class methods
	    //
	    //--------------------------------------------------------------------------
	    
	    /**
		 * Creates a custom browser popup window.
		 * 
		 * @param url The URL to load in the new browser window.
		 * @param name The name of the window to be created.  This name can be used to close the window using the <code>closePopup</code> method.
		 * @param props A generic <code>Object</code> that defines the new browser windows properties. 
		 */
		public static function createPopupWindow (url:String, name:String, props:Object=null):void {
			var propStr:String = "";
			if (props!=null) {
				for (var i:String in props) {
					propStr += i+"="+props[i]+",";
				}
				propStr = propStr.slice(0,-1);
			}
			if (!_windows.contains(name)) _windows.addItem(name);
			ExternalInterface.call("createPopupWindow",url,name,propStr);
		}
		
		/**
		 * Closes all browser windows created with the <code>createPopupWindow</code> method.
		 */ 
		public static function closeAllPopups():void {
			while (_windows.length>0) {
				closePopup(String(_windows.getItemAt(0)));
			}
		}
		
		/**
		 * Close a specific browser window created with the <code>createPopupWindow</code> method.
		 * 
		 * @param name The name of the window to be closed.  This was defined when the window was created by the <code>createPopupWindow</code> method.
		 */
		public static function closePopup(name:String):void {
			_windows.removeItemAt(_windows.getItemIndex(name));
			ExternalInterface.call("closePopup",name);
		}
	}
}